﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace XFSale.Aux
{
    public partial class DictValuePage : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string itemName = string.Empty;
                if (Request["itemName"] != null)
                {
                    itemName = Request["itemName"];
                    ddlItemName.SelectedValue = itemName;
                }

                dsDict.SelectParameters.Clear();
                dsDict.SelectParameters.Add("itemName", itemName);
            }
        }

        protected void gvDict_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        }

        protected void gvDict_RowCommand(object sender, GridViewCommandEventArgs e)
        {
        }

        protected void gvDict_RowEditing(object sender, GridViewEditEventArgs e)
        {
            e.Cancel = true;
            gvDict.Visible = false;
            pnlSearch.Visible = false;

            fvDict.PageIndex = gvDict.PageIndex * gvDict.PageSize + e.NewEditIndex;
            fvDict.ChangeMode(FormViewMode.Edit);
            fvDict.Visible = true;
        }

        protected void fvDict_PreRender(object sender, EventArgs e)
        {
            Button btnCtrl = null;
            TextBox txtCtrl = null;
            DropDownList ddlCtrl = null;
            switch (fvDict.CurrentMode)
            {
                case FormViewMode.ReadOnly:
                    ChangeToShow();
                    break;

                case FormViewMode.Edit:
                    btnCtrl = fvDict.FindControl("btnInsert") as Button;
                    if (btnCtrl != null)
                    {
                        btnCtrl.Visible = false;
                    }

                    ddlCtrl = fvDict.FindControl("ddlItemName") as DropDownList;
                    ddlCtrl.Enabled = false;

                    break;

                case FormViewMode.Insert:
                    btnCtrl = fvDict.FindControl("btnUpdate") as Button;
                    if (btnCtrl != null)
                    {
                        btnCtrl.Visible = false;
                    }

                    ddlCtrl = fvDict.FindControl("ddlItemName") as DropDownList;
                    ddlCtrl.Enabled = true;
                    ddlCtrl.SelectedValue = ddlItemName.SelectedValue;

                    break;
            }
        }

        protected void fvDict_ItemCommand(object sender, FormViewCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "Cancel".ToUpper())
            {
                ChangeToShow();
            }
        }

        protected void fvDict_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
            dsDict.UpdateParameters.Add("old_itemName", Convert.ToString(e.OldValues["itemName"]));
            dsDict.UpdateParameters.Add("old_itemValue", Convert.ToString(e.OldValues["itemValue"]));
        }

        protected void fvDict_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
        {
            ChangeToShow();
        }

        protected void fvDict_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            dsDictTemp.SelectCommand = "select count(*) from XmDict where itemName=:itemName and itemDetail='1'";
            dsDictTemp.SelectParameters.Clear();
            dsDictTemp.SelectParameters.Add("itemName", Convert.ToString(e.Values["itemName"]));
            IEnumerator em = dsDictTemp.Select(new DataSourceSelectArguments()).GetEnumerator();
            em.MoveNext();
            DataRowView row = em.Current as DataRowView;
            int count = Convert.ToInt32(row[0]);
            if (count <= 0)
            {
                e.Cancel = true;
                ClientScript.RegisterStartupScript(GetType(), "Error", "alert('数据项名称不存在或者不允许明细');", true);
                return;
            }

            dsDictTemp.SelectCommand = "select count(*) from XmDictValue where itemName=:itemName and itemValue=:itemValue";
            dsDictTemp.SelectParameters.Clear();
            dsDictTemp.SelectParameters.Add("itemName", Convert.ToString(e.Values["itemName"]));
            dsDictTemp.SelectParameters.Add("itemValue", Convert.ToString(e.Values["itemValue"]));
            em = dsDictTemp.Select(new DataSourceSelectArguments()).GetEnumerator();
            em.MoveNext();
            row = em.Current as DataRowView;
            count = Convert.ToInt32(row[0]);
            if (count > 0)
            {
                e.Cancel = true;
                ClientScript.RegisterStartupScript(GetType(), "Error", "alert('数据项名称和取值重复');", true);
                return;
            }
        }

        protected void fvDict_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            ChangeToShow();
        }

        /*protected void btnSearch_Click(object sender, ImageClickEventArgs e)*/
        protected void ddlItemName_SelectedIndexChanged(object sender, EventArgs e)
        {
            dsDict.SelectParameters.Clear();
            dsDict.SelectParameters.Add("itemName", ddlItemName.SelectedValue);
            gvDict.DataBind();
        }

        protected void btnInsert_Click(object sender, ImageClickEventArgs e)
        {
            gvDict.Visible = false;
            pnlSearch.Visible = false;

            fvDict.Visible = true;
            fvDict.InsertItemTemplate = fvDict.EditItemTemplate;
            fvDict.ChangeMode(FormViewMode.Insert);
        }

        private void ChangeToShow()
        {
            gvDict.Visible = true;
            pnlSearch.Visible = true;
            fvDict.Visible = false;
        }
    }
}
